home
diamond Go Premium
Data Engineering Path  ·  PySpark

RDD - Action Aggregate

The aggregate() action is an advanced reduction operation. While standard reduce() requires the final output type to be identical to the RDD's element type, aggregate() allows you to return a completely different data type.

It is highly used for calculating compound metricslike calculating averages (which requires tracking both a sum total and a record count at the same time).


Syntax and Key Arguments

rdd.aggregate(zeroValue, seqOp, combOp)

  1. zeroValue: The initial identity value used for the accumulation in each partition and in the final merge (e.g. (0, 0) for sum and count).
  2. seqOp (Sequence Operator): A function applied locally within each partition to accumulate row values into the zeroValue.
  3. combOp (Combiner Operator): A function used to merge the accumulated values from different partitions together at the Driver node.

PySpark Code Example: Calculating Average

Let's calculate the average of a list of numbers. The elements are integers, but we want to return a tuple (sum, count) so we can divide them at the end.

Setup Spark Session

from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("RDD Action Aggregate") \
    .master("local[*]") \
    .getOrCreate()

sc = spark.sparkContext

Programmatic Accumulation:

# 1. Create a distributed RDD with 4 numbers split across 2 partitions
numbers_rdd = sc.parallelize([10, 20, 30, 40], numSlices=2)
print("Partition Layout:", numbers_rdd.glom().collect())
# Output: [[10, 20], [30, 40]]

# 2. Define seqOp: accumulates numbers locally inside each partition
# accum: a tuple (current sum, current count)
# value: the row element (integer)
def seq_op(accum, value):
    new_sum = accum[0] + value
    new_count = accum[1] + 1
    return (new_sum, new_count)

# 3. Define combOp: merges accumulated tuples from separate partitions together
# accum1, accum2: local tuples from partition 1 and partition 2
def comb_op(accum1, accum2):
    total_sum = accum1[0] + accum2[0]
    total_count = accum1[1] + accum2[1]
    return (total_sum, total_count)

# 4. Trigger the aggregate action
# Initial zero value is (sum=0, count=0)
final_sum, final_count = numbers_rdd.aggregate((0, 0), seq_op, comb_op)

# 5. Compute the final average
average = final_sum / final_count

print(f"Total Sum  : {final_sum}")   # 100
print(f"Total Count: {final_count}") # 4
print(f"Average    : {average}")     # 25.0
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.